home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / UTILITIE / UNIX_COH / 2774A.ZIP / UPTIME.CZ / UPTIME
Text File  |  1991-07-02  |  1KB  |  59 lines

  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <sys/stat.h>
  4. #include <sys/types.h>
  5.  
  6. /************************************************************************
  7.  
  8.         UPTIME - by Mark J. Quarles
  9.  
  10.  
  11.     This little program will report the elapsed time since the last
  12. boot of your system.
  13.  
  14.     It does it by getting the "last modify" time for the file 
  15. /etc/boottime, and then calculating the difference between that time and
  16. the current time.  The output is in the form of hh:mm:ss elapsed since the
  17. last boot of the system.
  18.  
  19.  ************************************************************************/
  20.  
  21. main()
  22. {
  23. long delta,current_time;
  24. int hours,minutes,seconds;
  25. struct stat statptr;
  26.  
  27. /*
  28.  * Get the time of last modification of the /etc/boottime file, which is
  29.  * created each time the system boots.
  30. */
  31.  
  32. if (stat("/etc/boottime",&statptr)==-1) {
  33.     printf("%cError: STAT call error\n",7);
  34.     exit(1);
  35.     }
  36.  
  37. time(¤t_time); /* get the current time */
  38.  
  39. printf("\nLast system boot was %s\n",ctime(&statptr.st_mtime));
  40.  
  41. delta=current_time-statptr.st_mtime; /* calculate the delta time */
  42.  
  43. /*
  44.  * Now, just convert the elapsed seconds since boot into hours/min/sec
  45.  * format
  46. */
  47.  
  48. hours=delta/3600;
  49. delta-=(hours*3600L);
  50.  
  51. minutes=delta/60;
  52. delta -= minutes*60L;
  53.  
  54. seconds=delta;
  55.  
  56. printf("System uptime is %02.2d:%02.2d:%02.2d\n\n",hours,minutes,seconds);
  57.  
  58. }
  59.